﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using BLL;
namespace BLL.IntelligenceSearch
{
    public class SearchOrganization : ASearch
    {
        private String organizationViewName = "V_organizationBRIEF";
        private List<String> organizationSearchWord = new List<string>();
        private int maxReturnCount = 10;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="searchword">搜索词分词后的列表</param>
        /// <param name="maxReturnCount">返回最大匹配搜索的个数，如果小于这个数量，则全部返回</param>
        public SearchOrganization(List<String> searchword, int maxReturnCount)
        {
            this.organizationSearchWord = searchword;
            this.maxReturnCount = maxReturnCount;
            InitFieldWeight();
        }
        /// <summary>
        /// 数据库中匹配到的字段的权重设置
        /// 以后可以更改这个方法，把配置保在文件中
        /// </summary>
        private void InitFieldWeight()
        {
            //如果需要配置每个字段匹配的权重，可以修改这个函数
            fieldWeight.Add("nickname", 2);
            fieldWeight.Add("fullname", 3);
            fieldWeight.Add("keyword1", 1);
            fieldWeight.Add("keyword2", 1);
            fieldWeight.Add("keyword3", 1);
        }

        public override ArrayList DoSearch()
        {
            if (organizationSearchWord.Count == 1 && organizationSearchWord[0].Length > 3
                && ToolBLL.isNumberic(organizationSearchWord[0]))
            {
                //当分词结果只有一个数字字符串，并且字符串长度>3时
                //猜测可能是多巴拉码
                //算法从多巴拉码中进行搜索，而不从公司名，昵称，关键词中搜索
                //TODO 重构这部分算法
                SearchDB("V_organizationBRIEF", "dblno", organizationSearchWord[0],"1");
            }
            else//否则从公司名，昵称，关键词中搜索
            {
                foreach (String s in organizationSearchWord)
                {

                    foreach (DictionaryEntry entry in fieldWeight)
                    {
                        SearchDB(organizationViewName, entry.Key.ToString(), s,"1");

                    }
                }
            }

            //返回maxReturnCount数量的数据
            int listCount = resultList.Count;
            if (listCount >= maxReturnCount)
            {
                return SortResultList(resultList).GetRange(0, maxReturnCount);//返回前十
            }
            else
            {
                return SortResultList(resultList).GetRange(0, listCount);
            }
        }

        public override ArrayList DoSearchAll()
        {
            if (organizationSearchWord.Count == 1 && organizationSearchWord[0].Length > 3
                && ToolBLL.isNumberic(organizationSearchWord[0]))
            {
                //当分词结果只有一个数字字符串，并且字符串长度>3时
                //猜测可能是多巴拉码
                //算法从多巴拉码中进行搜索，而不从公司名，昵称，关键词中搜索
                //TODO 重构这部分算法
                SearchDB("V_organizationBRIEF", "dblno", organizationSearchWord[0], "");
            }
            else//否则从公司名，昵称，关键词中搜索
            {
                foreach (String s in organizationSearchWord)
                {

                    foreach (DictionaryEntry entry in fieldWeight)
                    {
                        SearchDB(organizationViewName, entry.Key.ToString(), s, "");

                    }
                }
            }

            //返回maxReturnCount数量的数据
            int listCount = resultList.Count;
            if (listCount >= maxReturnCount)
            {
                return SortResultList(resultList).GetRange(0, maxReturnCount);//返回前十
            }
            else
            {
                return SortResultList(resultList).GetRange(0, listCount);
            }
        }
    }
}

